package org.rency.crawler.common.enums;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * 响应
 *
 * @author: user_rcy@163.com <br>Created on 2015年12月5日
 * @version: V1.0.0
 * @since JDK 1.8
 */
public enum ReturnCode {

  /************** 成功 **************/
  SUCCESS("S0000", "处理成功"),

  EXCEPTION("E0000","处理异常"),

  /************** 失败 **************/
  FAIL("F0000", "处理失败"),

  /************** 异常 **************/
  ILLEGAL_VALID("E1001", "参数校验未通过"),
  NOT_SUPPORT("E10002", "功能暂不支持"),
  STATUS_VALID("E1003", "状态校验未通过"),
  ADD_SEED_ERROR("E1004", "添加种子失败"),
  POLICY_REJECT("E1005", "策略拒绝"),
  CACHE_EXISTED("E1006", "缓存已存在"),

  /****************** 网络 ********************/
  HTTP_EXCEPTION("E2000","HTTP网络异常"),
  HTTP_METHOD_NOT_SUPPORT("E2001", "HTTP请求方式暂不支持"),
  HTTP_RETRY_TIMES_OUT("E2002", "HTTP请求超出重试次数"),
  INTERVAL_TIMEOUT("E2003", "服务器访问间隔未通过"),
  ROBOTS_REJECT("E2004", "Robots协议未通过"),
  HTTP_PROXY("E2005", "HTTP代理PROXY设置失败"),
  LOAD_DOCUMENT_EMPTY("E2006", "加载远程文档为空"),
  HTTP_REQUEST_TIMEOUT("E2007", "HTTP访问超时"),
  HTTP_NEED_RETRY("E2008", "HTTP访问需重试"),
  HTTP_NEEDNT_PARSE("E2009", "HTTP返回无需解析"),


  /****************** 解析 ********************/
  PARSER_ERROR("E3000","解析失败"),
  PARSER_FETCH_NOT_SUPPORT("E3001","页面不支持抓取"),

  /****************** 配置 ********************/
  LOAD_CONF("E4000","获取远程配置"),
  CONF_NOT_EXISTS("E4001","配置项不存在"),


  /****************** 代理 ********************/
  LOAD_PROXY("E5000","获取网络代理"),

  MACHINE_STATE_FAILED("F1000", "状态机异常"),
  ;

  private String code;
  private String msg;

  ReturnCode(String code, String msg) {
    this.code = code;
    this.msg = msg;
  }

  public String getCode() {
    return this.code;
  }

  public String getMsg() {
    return this.msg;
  }

  public static ReturnCode get(String code) {
    if (StringUtils.isBlank(code)) {
      return null;
    }
    for (ReturnCode ek : ReturnCode.values()) {
      if (ek.getCode().toUpperCase().equals(code.toUpperCase())) {
        return ek;
      }
    }
    throw new IllegalArgumentException("未知的状态:" + code);
  }

  @Override
  public String toString() {
    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  }
}